require 'rubygems'
require 'rake'

# Load this library's version information
require File.expand_path('../lib/merb-cache/version', __FILE__)

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
  spec.pattern = "spec/**/*_spec.rb"
  spec.fail_on_error = false
end

RSpec::Core::RakeTask.new(:rcov) do |spec|
  spec.pattern = "spec/**/*_spec.rb"
  spec.fail_on_error = false
  spec.rcov = true
end

task :default => :spec

task :doc => [:yard]
begin
  require 'yard'

  YARD::Rake::YardocTask.new do |t|
    t.files   = [File.join('lib', '**', '*.rb'), '-', File.join('docs', '*.mkd')]
    t.options = [
      '--output-dir', 'doc/yard',
      '--tag', 'overridable:Overridable',
      '--markup', 'markdown',
    ]
  end
rescue
end

##############################################################################
# memcached
##############################################################################

MEMCACHED_PORTS = 43042..43043

namespace :memcached do
  desc "Start the memcached instances for specs"
  task :start do
    log = "/tmp/memcached.log"
    system ">#{log}"

    verbosity = (ENV['DEBUG'] ? "-vv" : "")

    (MEMCACHED_PORTS).each do |port|
      system "memcached #{verbosity} -p #{port} >> #{log} 2>&1 &"
    end
  end

  desc "Kill the memcached instances"
  task :kill do
    `ps awx`.split("\n").grep(/#{MEMCACHED_PORTS.to_a.join('|')}/).map do |process| 
      system("kill -9 #{process.to_i}") rescue nil
    end
  end
end
